package com.androturka.yesiltuyo;

import java.util.Random;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.Resources;
import android.util.Log;
import android.widget.RemoteViews;
import com.androturka.yesiltuyo.R;

public class GreenHintsWidget extends AppWidgetProvider {

	public static String TAG = "GreenHintsWidget";
	
	// our actions for our buttons
	public static String ACTION_WIDGET_LEFT = "ActionReceiverLeft";
	public static String ACTION_WIDGET_RIGHT = "ActionReceiverRight";
	public static String ACTION_WIDGET_ABOUT = "ActionReceiverAbout";
	public static String ACTION_WIDGET_SHARE = "ActionReceiverShare";
	
	private static final String DEBUG_TAG = "debug";
	private static final String HINT_ID = "hintid";
	private static final String PREFS_NAME = "HINTS_ID";
    private static SharedPreferences appSharedPrefs;
    private static Editor prefsEditor;
	
	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {

		try {
			updateWidgetContent(context, appWidgetManager);
		} catch (Exception e) {
			Log.e(DEBUG_TAG, "Failed");
		}
	}

	public static void updateWidgetContent(Context context,
			AppWidgetManager appWidgetManager) {
		
		Log.d(DEBUG_TAG, "updateWidgetContent");
		
		Resources res = context.getResources();
		String[] green_hints = res.getStringArray(R.array.green_hints_array);
		int sizeOfArray = green_hints.length;
		
		Random rand = new Random();
		int randomNum = 0;
		randomNum = rand.nextInt(sizeOfArray) ;
		
		
		Log.d(TAG,"new hintid is " + randomNum);
		RemoteViews remoteView = new RemoteViews(context.getPackageName(),
				R.layout.appwidget_layout);
		remoteView.setTextViewText(R.id.textBox, green_hints[randomNum]);

		ComponentName testAppWidget = new ComponentName(context,
				GreenHintsWidget.class);

		Intent intentLeft = new Intent(context, GreenHintsWidget.class);
		intentLeft.setAction(ACTION_WIDGET_LEFT);
	    PendingIntent pendingIntentLeft = PendingIntent.getBroadcast(context, 0, intentLeft, 0);
	    remoteView.setOnClickPendingIntent(R.id.left, pendingIntentLeft);
		
	    Intent intentRight = new Intent(context, GreenHintsWidget.class);
	    intentRight.setAction(ACTION_WIDGET_RIGHT);
	    PendingIntent pendingIntentRight = PendingIntent.getBroadcast(context, 0, intentRight, 0);
	    remoteView.setOnClickPendingIntent(R.id.right, pendingIntentRight);
	    
		
	    Intent intentShare = new Intent(context, GreenHintsWidget.class);
	    intentShare.setAction(ACTION_WIDGET_SHARE);
	    PendingIntent pendingIntentShare = PendingIntent.getBroadcast(context, 0, intentShare, 0);
	    remoteView.setOnClickPendingIntent(R.id.logo, pendingIntentShare);
	    /*
	    Intent intentShare = new Intent();
	    intentShare.setAction(Intent.ACTION_SEND);
		intentShare.setType("text/plain");
		intentShare.putExtra(Intent.EXTRA_TEXT, green_hints[randomNum]);
		intentShare.putExtra(Intent.EXTRA_SUBJECT, "R.string.headerText");
	    PendingIntent pendingIntentShare = PendingIntent.getBroadcast(context, 0, Intent.createChooser(intentShare, "Paylaş"), 0);
	    remoteView.setOnClickPendingIntent(R.id.logo, pendingIntentShare);
	    */
	    
		appSharedPrefs = context.getSharedPreferences(PREFS_NAME, 0);
		prefsEditor = appSharedPrefs.edit();
		prefsEditor.putInt(HINT_ID, randomNum);
		prefsEditor.commit();
		
		appWidgetManager.updateAppWidget(testAppWidget, remoteView);

	}

	@Override
	public void onReceive(Context context, Intent intent) {
	    AppWidgetManager appWidgetManager;
		if (( intent.getAction()!= null) && 
		    intent.getAction().equals(ACTION_WIDGET_LEFT)) {
	        Log.i("onReceive", ACTION_WIDGET_LEFT);
			Resources res = context.getResources();
			String[] green_hints = res.getStringArray(R.array.green_hints_array);
			int sizeOfArray = green_hints.length;

			int hintid = appSharedPrefs.getInt(HINT_ID, 0);

			int newhintid = --hintid;
			if ( newhintid < 0 )
				newhintid = sizeOfArray - 1;
			
			Log.d(TAG,"new hintid is " + newhintid);
			prefsEditor.putInt(HINT_ID, newhintid);
			prefsEditor.commit();
			
	        RemoteViews remoteView = new RemoteViews(context.getPackageName(),
					R.layout.appwidget_layout);
			remoteView.setTextViewText(R.id.textBox, green_hints[newhintid]);

			ComponentName testAppWidget = new ComponentName(context,
					GreenHintsWidget.class);

		    appWidgetManager = AppWidgetManager.getInstance(context); 
		    appWidgetManager.updateAppWidget(testAppWidget, remoteView);
		
	    } else if (( intent.getAction()!= null) && 
			        intent.getAction().equals(ACTION_WIDGET_RIGHT)) {
	    	
	        Log.i("onReceive", ACTION_WIDGET_RIGHT);

	        Resources res = context.getResources();
			String[] green_hints = res.getStringArray(R.array.green_hints_array);
			int sizeOfArray = green_hints.length;

			int hintid = appSharedPrefs.getInt(HINT_ID, 0);

			int newhintid = ++hintid;
			if ( newhintid >= sizeOfArray )
				newhintid = 0;

			Log.d(TAG,"new hintid is " + newhintid);

			prefsEditor.putInt(HINT_ID, newhintid);
			prefsEditor.commit();
			
	        RemoteViews remoteView = new RemoteViews(context.getPackageName(),
					R.layout.appwidget_layout);
			remoteView.setTextViewText(R.id.textBox, green_hints[newhintid]);

			ComponentName testAppWidget = new ComponentName(context,
					GreenHintsWidget.class);

		    appWidgetManager = AppWidgetManager.getInstance(context); 
		    appWidgetManager.updateAppWidget(testAppWidget, remoteView);
			 			 
	    } else if (( intent.getAction()!= null) && 
			          intent.getAction().equals(ACTION_WIDGET_SHARE)) {
	    	
	        Log.i("onReceive", ACTION_WIDGET_SHARE);

	        Resources res = context.getResources();
			String[] green_hints = res.getStringArray(R.array.green_hints_array);

			int hintid = appSharedPrefs.getInt(HINT_ID, 0);
			
			String shareText = green_hints[hintid] + " " + context.getString(R.string.shareText);
			String shareSubject = context.getString(R.string.shareSubject);
			
		    Intent intentShare = new Intent();
		    intentShare.setAction(Intent.ACTION_SEND);
		    intentShare.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			intentShare.setType("text/plain");
			intentShare.putExtra(Intent.EXTRA_TEXT, shareText ) ;
			intentShare.putExtra(Intent.EXTRA_SUBJECT, shareSubject);
			Intent intentChooser = Intent.createChooser(intentShare, "Paylaş"); 
		    intentChooser.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		    context.startActivity(intentChooser);
			 			 
	    } else {
	        super.onReceive(context, intent);
	    }
	}	
}